/**
 * @(#)BasicoResourceBundleCombinado.java Jan 31, 2007
 *
 * Copyright (c) 2005-2010 Psiqueware S.A. de C.V. All Rights Reserved.
 * This software is the proprietary information Psiqueware S.A. de C.V.
 * Use is subject to license terms.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 * <br/>Usuario : AngelElectrico
 * <br/>Creaci&oacute;n: Jan 31, 2007 - 9:53:47 AM
 * <br/>
 * <br/>
 * <br/><t5>Cambios :</t5>
 * <br/><b>Fecha | Autor  |  Cambios</b>
 * <ul>
 * <li>Jan 31, 2007 | <a href = "mailto:AngelElectrico@PsiqueWare.com">Angel El&eacute;ctrico</a> | Creaci&oacute;n del Objeto.</li>
 *
 * </ul>
 */

package com.psiqueware.alheli.i18n.propertyBundles;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;

/**
 * Bundle de recursos personalizado.
 * <br/>Paquete Original: com.psiqueware.basico.i18n
 * <br/>Nombre  Original: BasicoResourceBundleCombinado
 * <br/>Oprima para ver : <a href="BasicoResourceBundleCombinado.java.html"><i>C&oacute;digo Fuente</i> </a>
 *
 * @author <a href="mailto:AngelElectrico@PsiqueWare.com">Angel El&eacute;ctrico</a>
 * @version Revision: 1.1 Jan 31, 2007 9:53:47 AM
 * @noinspection ClassWithoutLogger
 */
public final class BasicoResourceBundleCombinado extends ResourceBundle
{

    /**
     * Mapa con los recursos de este bundle.
     */
    private Map<String, Object> m_recursos = new HashMap<String, Object>();

    /**
     * Locale para este Bundle.
     */
    private final Locale m_locale;

    /**
     * Constructor de BasicoResourceBundleCombinado.
     *
     * @param locale para este Bundle.
     */
    public BasicoResourceBundleCombinado(Locale locale)
    {
        this.m_locale = locale;
    }

    /**
     * Agrega todos los recursos de un Bundle a este Bundle.
     *
     * @param bundle {@link ResourceBundle} cuyos recursos se van a agregar.
     */
    public void agregarRecursos(ResourceBundle bundle)
    {
        final Enumeration<String> keys = bundle.getKeys();
        while (keys.hasMoreElements())
        {
            String key = keys.nextElement();
            m_recursos.put(key, bundle.getObject(key));
        }
    }

    /**
     * Returns the locale of this resource bundle. This method can be used after a
     * call to getBundle() to determine whether the resource bundle returned really
     * corresponds to the requested locale or is a fallback.
     *
     * @return the locale of this resource bundle
     */
    @Override
    public Locale getLocale()
    {
        return m_locale;
    }

    /**
     * Gets an object for the given key from this resource bundle.
     * Returns null if this resource bundle does not contain an
     * object for the given key.
     *
     * @param key the key for the desired object
     *
     * @return the object for the given key, or null
     *
     * @throws NullPointerException if {@link key} is <code>null</code>
     * @noinspection EmptyCatchBlock
     */
    protected Object handleGetObject(String key)
    {
        Object salida = m_recursos.get(key);
        if (null == salida)
            throw new MissingResourceException("<error><msg>No se pudo recuperar el recurso.</msg><param>" + key + "</param></error>", this.getClass().getCanonicalName(), key);
        return salida;
    }

    /**
     * Returns an enumeration of the m_keys.
     *
     * @noinspection MethodWithMultipleLoops, unchecked
     */
    public Enumeration<String> getKeys()
    {
        final Set<String> strings = m_recursos.keySet();
        final EnumerationAgregable keys = new EnumerationAgregable();
        for (String key : strings)
        {
            keys.addKey(key);
        }
        return keys;
    }


    /**
     * Convierte este {@link BasicoResourceBundleCombinado} a su representaci&oacute;n textual.
     *
     * @noinspection DuplicateStringLiteralInspection, HardCodedStringLiteral, HardcodedLineSeparator, MagicNumber @return con el contenido de un archivo.properties para este Bundle.
     */
    public String toString()
    {
        StringBuilder sb = new StringBuilder(1000);
        final Set<Map.Entry<String, Object>> entries = m_recursos.entrySet();
        for (Map.Entry<String, Object> entry : entries)
        {
            sb.append(entry.getKey()).append("=").append(entry.getValue()).append("\n");
        }
        return sb.toString();
    }


    /**
     * Convierte este {@link BasicoResourceBundleCombinado} a su representaci&oacute;n textual.
     *
     * @noinspection DuplicateStringLiteralInspection, HardCodedStringLiteral, HardcodedLineSeparator
     */
    public String toString2()
    {
        StringBuilder sb = new StringBuilder();
        sb.append("<Instance Class = '").append(this.getClass().getCanonicalName()).append("'>");
        sb.append("<recursos>").append(m_recursos).append("</recursos> ");
        sb.append("</Instance>");
        return sb.toString();
    }
}

